package com.lm;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import mapgenerator.Main;

/**
 *
 * @author Sajarwo Anggai
 */
public class GridWorkspace extends JPanel implements Runnable {

    private Main comp;
    private int tileW, tileH;
    private int incX, incY, pttW, pttH;
    private BufferedImage gridWorkspace;
    private BufferedImage layerUp;
    private Color color;
    private Thread t;

    public GridWorkspace(Main comp) {
        this.comp = comp;
        color = new Color(0, 0, 0, 0);
        setDoubleBuffered(false);
        setLayout(null);
        t = new Thread(this);
        t.start();
    }

    public void drawGrid() {
        if (gridWorkspace == null) {
            gridWorkspace = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
        }
        tileW = comp.project.getTileWidth();
        tileH = comp.project.getTileHeight();
        Graphics2D g2d = (Graphics2D) gridWorkspace.getGraphics();
        g2d.setBackground(color);
        g2d.clearRect(0, 0, 1000, 1000);
        Font font = new Font("SansSerif", Font.PLAIN, tileH / 4);
        g2d.setFont(font);
        FontRenderContext fontRenderContext = g2d.getFontRenderContext();
        for (int i = 0; i < 30; i++) {
            g2d.setColor(Color.BLACK);
            g2d.drawLine(i * tileW, 0, i * tileW, getHeight());
            g2d.drawLine(0, i * tileH, getWidth(), i * tileH);
            for (int j = 0; j < 30; j++) {
                g2d.setColor(Color.BLACK);
                String coords = "(" + (i + incX) + "," + (j + incY) + ")";
                Rectangle2D textSize = font.getStringBounds(coords, fontRenderContext);
                int x = i * tileW + (int) ((tileW - textSize.getWidth()) / 2);
                int y = j * tileH + (int) ((tileH + textSize.getHeight()) / 2);
                g2d.drawString(coords, x, y);
            }
        }
        drawAll();
    }

    private void draw(Graphics2D g2d) {
        g2d.drawImage(layerUp, null, 0, 0);
        g2d.drawImage(gridWorkspace, null, 0, 0);
        //comp.ScrollPaneWorkspaceRender.repaint();
    }

    public void drawLayer() {
        if (layerUp == null) {
            layerUp = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
        }
        Graphics2D g2d = (Graphics2D) layerUp.getGraphics();
        g2d.setBackground(color);
        g2d.clearRect(0, 0, 1000, 1000);

        LayerContainer lc = comp.layerContainer;
        int pttX = WorkSpace.pixelsToTiles(getWidth(), comp.project.getTileWidth());
        int pttY = WorkSpace.pixelsToTiles(getHeight(), comp.project.getTileHeight());
        System.out.println("INC = " + incX + "," + incY);
        System.out.println("PTT = " + pttW + "," + pttH);

        for (int i = 0; i < lc.getLayers().size(); i++) {
            Layer lm = (Layer) lc.getLayers().get(i);
            if (lm.isShow()) {
                lm.draw(g2d, incX, incY, pttW, pttH, comp.project.getTileWidth(), comp.project.getTileHeight());
                System.out.println("ok" + lm.getName());
            }

        }
        drawAll();
    }

    public void drawLayer(int tx, int ty) {
        if (layerUp == null) {
            layerUp = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
        }
        Graphics2D g2d = (Graphics2D) layerUp.getGraphics();
        g2d.setBackground(color);

        int xx = tx - WorkSpace.pixelsToTiles(this.getX(), tileW);
        int yy = ty - WorkSpace.pixelsToTiles(this.getY(), tileH);
        g2d.clearRect(xx * tileW, yy * tileH, tileW, tileH);

        LayerContainer lc = comp.layerContainer;
        for (int i = 0; i < lc.getLayers().size(); i++) {
            Layer lm = (Layer) lc.getLayers().get(i);
            if (lm.isShow()) {
                lm.draw(g2d, tx, ty, xx, yy, tileW, tileH, true);
            }
        }
        g2d.dispose();
        drawAll();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        //antialiasing
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);


        // smoother (and slower) image transforms  (e.g., for resizing)
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        draw(g2d);

    }

    public void drawAll() {
        paintComponent(this.getGraphics());
    }

    public void setInc(int incX, int incY, int W, int H) {
        this.incX = incX;
        this.incY = incY;
        this.pttW = W;
        this.pttH = H;
    }
    boolean draw = false;

    public void setDraw(boolean draw) {
        this.draw = draw;
    }

    @Override
    public void run() {
        while (true) {
            try {
                if (draw) {
                    drawAll();
                    draw = false;
                }
                Thread.sleep(20);
            } catch (InterruptedException ex) {
                System.out.println("interupted");
            }
        }
    }

    public void interupt() {
        t.interrupt();
    }
}